<?php

namespace Forms\User;

/**
 * 用户登录表单
 */
class LoginModel extends \Forms\AbstractModel {

    /**
     * 表单字段
     * 
     * @var array
     */
    protected $_fields = array(
        'mobile'   => array(
            'label'    => '手机号码',
            'name'     => 'mobile',
            "validate" => array(
                array("type" => "string", "min" => "11", "max" => "11", "msg" => "手机号码长度不正确")
            ),
        ),
        'password' => array(
            'label'    => '密码',
            'name'     => 'password',
            "validate" => array(
                array("type" => "string", "min" => "6", "max" => "18", "msg" => "密码长度6到18位")
            ),
        )
    );

    /**
     * 校验手机号码
     * 
     * @return boolean
     */
    public function validateMobile() {
        $mobile = $this->getFieldValue('mobile');
        if (!$mobile) {
            $this->setFieldMessage('mobile', \Text\TextModel::getText(300001));
            return false;
        }

        //国内
        if (preg_match('/^1[3|4|5|7|8][0-9]\d{8}$/i', $mobile)) {
            return true;
        }

        //台湾
        if (preg_match('/^88609\d{8}$/i', $mobile)) {
            return true;
        }

        //香港
        if (preg_match('/^852\d{8}$/i', $mobile)) {
            return true;
        }

        //澳门
        if (preg_match('/^853\d{8}$/i', $mobile)) {
            return true;
        }

        $this->setFieldMessage('mobile', \Text\TextModel::getText(300002));
        return false;
    }

}
